package com.xjw.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.xjw.dao.UserDao;
import com.xjw.entity.User;

/**
 * 
	 * 项目名称：spring-project  
	 * 实现功能：  
	 * 类名称：UserDaoImpl   
	 * 版权所有：
	 * 创建人：徐纪伟 
	 * E-mail: 289045706@qq.com
	 * 创建时间：2016年12月6日 下午3:41:38   	
	 * 修改人：   
	 * 修改时间：   
	 * 修改备注：   
	 * @version
 */
@Repository
public class UserDaoImpl implements UserDao {

	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void create(User user) {
		jdbcTemplate.update("insert into test_user values(?,?,?)",new Object[]{user.getUsername(),user.getPassword()});
		
	}

	public void delete(User user) {
		jdbcTemplate.update("delete from test_user where user_name=?",new Object[]{user.getUsername()});
		
	}

	public User get(String username) {
		final User user = new User();
		jdbcTemplate.query("select * from test_user where user_name=?", new Object[]{username},new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				user.setUsername(rs.getString("user_name"));
				user.setPassword(rs.getString("user_pwd"));
				if (rs.getObject("age") != null) {
					user.setAge(rs.getInt("age"));
				}
			}
		});
		return user;
	}

	public List<User> list() {
		List<User> users = new ArrayList<User>();
		List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from test_user");
		Iterator<Map<String, Object>> it = list.iterator();
		User user = null;
		Map<String, Object> userMap = null;
		while (it.hasNext()) {
			userMap = (Map<String, Object>) it.next();   
			user = new User();
			user.setUsername(userMap.get("user_name").toString());
			user.setPassword(userMap.get("user_pwd").toString());
			users.add(user);
		}
//		final List<User> users = new ArrayList<User>();
//		final User user = new User();
//		jdbcTemplate.query("select * from users", new Object[]{},new RowCallbackHandler() {
//			public void processRow(ResultSet rs) throws SQLException {
//				while(rs.next()){
//					user.setUsername(rs.getString("username"));
//					user.setPassword(rs.getString("password"));
//					user.setRealname(rs.getString("realname"));
//					user.setAge(rs.getInt("age"));
//					users.add(user);
//				}
//			}
//		});
		
		return users;
	}

	public void update(User user) {
		jdbcTemplate.update("update test_user set password=? where user_name=?",new Object[]{user.getPassword(), user.getUsername()});
		
	}

	public int count() {
		// TODO Auto-generated method stub
		
		//return jdbcTemplate.queryForInt("select count(*) from users");
		return 0;
	}

}
